package org.blogwu.learn.multithread.chapter2.demo9;


/**
 * 此demo 证明sync内部 还会发生重排序
 *
 * @author hyf
 * @date 2018/11/29
 **/
public class Main4 {

    //volatile
    static volatile Boolean bool;

    static Object o1 = new Object();

    static Object o2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("进入临界区");
            synchronized (Main4.class) {
                while (bool) {
//                    Thread.yield();
                }
                System.out.println("over1");
            }

        });

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Hello World");
            bool = false;
//            synchronized (o2) {
//                System.out.println("Hello World");
//                bool = false;
//            }
            System.out.println("over2");

        });

        t1.start();
        t2.start();
        bool = true;
    }

}
